<?php
/**
 * 任务处理类
 * @author ice<iceinto@mallog.com.cn>
 */
class TaskAction extends CommonAction
{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('nav', 4);
    }

    /**
     * 任务列表
     * @author ice<iceinto@mallog.com.cn>
     */
    public function index()
    {
        $TaskModel = D('Task');
        $Page = $this->getPage($TaskModel->count());
        $tasks = $TaskModel->getList(null, $Page->firstRow . ',' . $Page->listRows);
        $this->assign('tasks', $tasks);
        $this->assign('page', $Page->show());
        $this->display();
    }

    /**
     * 只显示任务详细 - 不给予任何操作
     * 根据任务不同状态进行不同显示
     * @author ice<iceinto@mallog.com.cn>
     */
    public function detail()
    {
        $id = intval(getGet('id'));

        $TaskModel = D('Task');
        $task = $TaskModel->getById($id, '*');
        if (!$task) {
            exit;
        }
        $status = intval($task['status']);
        $QueueModel = D('Queue');
        $TaskAllotModel = D('TaskAllot');
        $UserModel = D('User');
        //Get allot
        $allot = $TaskAllotModel->getOneByTid($id, 'id,create_time,uid,status,start_time,end_time,image');
        if ($allot) {
            if ($status != 1 && $status != 100) {
                $queue = $QueueModel->field('creator,executor,auditor')->where('tid=' . $id)->find();
                if ($queue) {
                    $queue['creator_num'] = $UserModel->getNum($queue['creator']);
                    if ($queue['executor']) {
                        $queue['executor_num'] = $UserModel->getNum($queue['executor']);
                    }
                    if ($queue['auditor']) {
                        $queue['auditor_num'] = $UserModel->getNum($queue['auditor']);
                    }
                }
            }
            if ($status == 100) {
                $queue['creator_num'] = $UserModel->getNum($task['uid']);
                $queue['executor_num'] = $UserModel->getNum($allot['uid']);
                $audit = D('TaskAudit')->field('uid')->where('tid=' . $id)->find();
                $queue['auditor_num'] = $UserModel->getNum($audit['uid']);
            }
            //Get allot image
            if ($allot['image']) {
                $allot['image_url'] = $this->getManagedUrl($allot['image'], 'task');
            }
            //Get audit return
            $TaskAuditReturnModel = D('TaskAuditReturn');
            unset($map);
            $map['tid'] = $id;
            $auditReturns = $TaskAuditReturnModel->where($map)->field('id,uid,remark,create_time,reply,reply_time,image')->select();
            if ($auditReturns) {
                foreach ($auditReturns as $key => $v) {
                    if ($v['image']) {
                        $auditReturns[$key]['image_url'] = $this->getManagedUrl($v['image'], 'task');
                    }
                }
            }
            $this->assign('queue', $queue);
        }
        //Get return
        $returns = D('TaskReturn')->getAllReturn($id);

        $task['status_info'] = $QueueModel->getStatus($status);
        $task = D('Mall')->setAddress($task);
        $phones = D('MallPhone')->getList($task['mid'], $task['fid'], $task['bid']);

        $this->assign('auditReturns', $auditReturns);
        $this->assign('returns', $returns);
        $this->assign('allot', $allot);
        $this->assign('phones', $phones);
        $this->assign('task', $task);
        $this->display();
    }

    public function delete()
    {

    }
}

?>